﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddDeliveryArea : ASPXBaseClass
{
    GZ.Entity.EDeliveryArea objDeliveryArea = new GZ.Entity.EDeliveryArea();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"].ToString() != "admin")
        {
            Response.Redirect("~/Admin/RedirectLogin.htm");
        }
        if (!IsPostBack)
        {
            BindDrp();
            if (Request.QueryString["Id"] != null)
            {
                Bind();
            }
        }
    }

    void Bind()
    {
        string Id = GZ.Common.HttpHelper.GetParameter("Id", GZ.Common.HttpHelper.HttpType.GET);
        List<GZ.Entity.EDeliveryArea> list = objDeliveryArea.GetById(Convert.ToInt32(Id));

        this.txtDefaultCompany.Text = list[0].DefaultCompanyName;
        this.txtFloorName.Text = list[0].DeliveryAreaName;
        this.txtOrderValue.Text = list[0].OrderValue.ToString();
        this.drpOfficeId.SelectedValue = list[0].ParentId.ToString();
        list = objDeliveryArea.GetById(Convert.ToInt32(this.drpOfficeId.SelectedValue));
        this.drpBusinessDistrictId.SelectedValue = list[0].ParentId.ToString();
        list = objDeliveryArea.GetById(Convert.ToInt32(this.drpRegion.SelectedValue));
        this.drpRegion.SelectedValue = list[0].ParentId.ToString();
    }


    void BindDrp()
    {
        GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(0), this.drpRegion);
        if (this.drpRegion.SelectedValue.Length == 0)
        {
            this.drpBusinessDistrictId.Items.Add(new ListItem("请选择", "-1"));
        }
        else
        {
            GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(Convert.ToInt32(this.drpRegion.SelectedValue)), this.drpBusinessDistrictId);
        }
        if (this.drpBusinessDistrictId.SelectedValue.Length == 0)
        {
            this.drpOfficeId.Items.Add(new ListItem("请选择", "-1"));
        }
        else
        {
            GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(Convert.ToInt32(this.drpBusinessDistrictId.SelectedValue)), this.drpOfficeId);
        }
    }

    /// <summary>
    /// 保存事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        GZ.Entity.EDeliveryArea obj = new GZ.Entity.EDeliveryArea();
        if (GZ.Common.HttpHelper.GetParameter("Id", GZ.Common.HttpHelper.HttpType.GET).Length == 0)
        {
            obj.DefaultCompanyName = this.txtDefaultCompany.Text;
            obj.DeliveryAreaName = this.txtFloorName.Text;
            obj.ParentId = Convert.ToInt32(this.drpOfficeId.SelectedValue);
            obj.OrderValue = Convert.ToInt32(this.txtOrderValue.Text);
            if (objDeliveryArea.Add(obj))
            {
                this.txtOrderValue.Text = string.Empty;
                this.txtDefaultCompany.Text = string.Empty;
                this.txtFloorName.Text = string.Empty;
            }
        }
        else
        {
            obj.DefaultCompanyName = this.txtDefaultCompany.Text;
            obj.DeliveryAreaName = this.txtFloorName.Text;
            obj.ParentId = Convert.ToInt32(this.drpOfficeId.SelectedValue);
            obj.OrderValue = Convert.ToInt32(this.txtOrderValue.Text);
            obj.Id = Convert.ToInt32(GZ.Common.HttpHelper.GetParameter("Id", GZ.Common.HttpHelper.HttpType.GET));
            if (objDeliveryArea.Edit(obj))
            {
                this.txtOrderValue.Text = string.Empty;
                this.txtDefaultCompany.Text = string.Empty;
                this.txtFloorName.Text = string.Empty;
            }
        }

        ClientScript.RegisterClientScriptBlock(this.GetType(), "update", "<script>window.opener.location.href=window.opener.location.href;window.close();</script>");
    }

    /// <summary>
    /// 根据区域绑定商圈
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(Convert.ToInt32(this.drpRegion.SelectedValue)), this.drpBusinessDistrictId);
        if (this.drpBusinessDistrictId.Items.Count == 0)
        {
            this.drpBusinessDistrictId.Items.Add(new ListItem("请选择", "-1"));
            this.drpOfficeId.Items.Clear();
            this.drpOfficeId.Items.Add(new ListItem("请选择", "-1"));
        }
        else
        {
            GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(Convert.ToInt32(this.drpBusinessDistrictId.SelectedValue)), this.drpOfficeId);
        }
    }

    /// <summary>
    /// 根据商圈绑定写字楼
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpBusinessDistrictId_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpBusinessDistrictId.SelectedValue == "-1")
        {
            this.drpOfficeId.Items.Add(new ListItem("请选择", "-1"));
        }
        else
        {
            GZ.Common.DataBindHelper.BindDropDownList("DeliveryAreaName", "Id", objDeliveryArea.GetDeliveryArea(Convert.ToInt32(this.drpBusinessDistrictId.SelectedValue)), this.drpOfficeId);
            if (this.drpOfficeId.Items.Count == 0)
            {
                this.drpOfficeId.Items.Add(new ListItem("请选择", "-1"));
            }

        }
    }

    void Edit(string deliveryAreaId)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "editRegion" + deliveryAreaId, "<script>window.open('EditDeliveryArea.aspx?Id=" + deliveryAreaId + "', 'edit" + deliveryAreaId + "', 'height=500,width=600,top=300,left=400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no')</script>");
    }

    protected void lbtnRegion_Click(object sender, EventArgs e)
    {
        Edit("0");
    }
    protected void ibtnBussiness_Click(object sender, EventArgs e)
    {
        Edit(this.drpRegion.SelectedValue);
    }
    protected void ibtnOffice_Click(object sender, EventArgs e)
    {
        Edit(this.drpBusinessDistrictId.SelectedValue);
    }
}